<template>
	<div>
		<uni-indexed-list :options="list" :showSelect="false" @click="bindClick"></uni-indexed-list>
	</div>
</template>

<script>
import { apiGet } from '@/api/openapi.js'
	export default {
		data() {
			return {
				list:[{
					"letter": "北京",
					"data": [
						"阿克苏机场",
						"阿拉山口机场",
						"阿勒泰机场",
						"阿里昆莎机场",
						"安庆天柱山机场",
						"澳门国际机场"
					]
				}, {
					"letter": "广州",
					"data": [
						"保山机场",
						"包头机场",
						"北海福成机场",
						"北京南苑机场",
						"北京首都国际机场"
					]
				}],
				more: 'noMore',
				loaded: 0,
				param:{
					pageNum: 0,
					pageSize: 10,
					orderByColumn: 'id',
					isAsc: 'DESC'
				},
				userList:[],
				deptList:[],
				info:{
					content:'',
				}
			}
		},
		onLoad() {
			this.getList();
		},
		methods:{
			getList() {
				apiGet({"@table":"sys_dept","status":0,"@pager":{limit:1000}}).then(response => {
					let deptList = [];
					for(var i=0;i<response.data.rows.length;i++){
						deptList.push({data:[],letter:response.data.rows[i]['dept_name'], deptId:response.data.rows[i]['dept_id']});
					}
					apiGet({"@table":"sys_user","status":0, "del_flag":"0", "@pager":{limit:10000}, "@column":"user_id,nick_name,dept_id,phonenumber,email"}).then(res => {
						for(var j=0;j<res.data.rows.length;j++){
							for(var k=0;k<deptList.length;k++){
								if(deptList[k]['deptId'] == res.data.rows[j]['dept_id']){
									deptList[k]['data'].push(res.data.rows[j]['nick_name']);
								}
							}
						}
						this.list = deptList;
						this.userList = res.data.rows;
					});
					this.deptList = response.data.rows;
				});			

			},
			bindClick(c) {
				let deptId = null;
				console.log(c)
				for(var i=0;i<this.deptList.length;i++){
					if(this.deptList[i]['dept_name'] == c.item.key){
						deptId = this.deptList[i]['dept_id'];
						break;
					}
				}
				
				if(deptId == null){
					console.log("no dept");
					return;
				}
				
				let user = null;
				for(var i=0;i<this.userList.length;i++){
					if(this.userList[i]['dept_id'] == deptId && this.userList[i]['nick_name'] == c.item.name){
						user = this.userList[i];
						break;
					}
				}				
				
				let actionList = [user.phonenumber];
				
				// #ifndef H5
				actionList.push('拨号');
				// #endif
				
				uni.showActionSheet({
					itemList: actionList,
					success: function (res) {
						if(res.tapIndex == 0){
							uni.setClipboardData({
								data: user.phonenumber,
								success: function () {
									uni.showToast({
										title: user.phonenumber+' 已复制',
										duration: 2000
									});
								}
							});
						}else if(res.tapIndex == 1){
							uni.makePhoneCall({
								phoneNumber: user.phonenumber
							});
						}
					},
					fail: function (res) {
						console.log(res.errMsg);
					}
				});
			},
			dialogConfirm(done) {
				this.$refs.popup.open();
				done();
			}
		}
	}
</script>

<style>
</style>
